home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8182 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  102 lines

  1. Path: ix.netcom.com!netnews
  2. From: fedinv@ix.netcom.com(Steve Mahoney )
  3. Newsgroups: comp.lang.c++
  4. Subject: OOD problem
  5. Date: 15 Feb 1996 15:49:49 GMT
  6. Organization: Netcom
  7. Message-ID: <4fvkmt$ems@reader2.ix.netcom.com>
  8. NNTP-Posting-Host: ix-pit1-26.ix.netcom.com
  9. X-NETCOM-Date: Thu Feb 15  7:49:49 AM PST 1996
  10.  
  11.  
  12. I have two classes which form a handle/body pair. So, one class ( class
  13. A, the handle) is seen by clients, and class A HAS-A class B ( the body
  14. ) which is hidden from clients. Hence we have the following situation -
  15.  
  16. // ...Handle
  17. class A 
  18. {
  19.     A( int n ) { if  ( n == x ) b = new ( C ) ;
  20.             elseif ( n == y ) b = new ( D ) ;
  21.             else b = new (E) ; } ;         
  22.     ~A() { } ;
  23.     .
  24.     .
  25.     A_f1 ( )  { b->B_f1( ) ; } ;  // forward request to B
  26.     A_f2 ( )  { b->B_f2( ) ; } ;  //    ô   ô
  27.     .
  28.     .
  29.      private:
  30.     B   *b ;   // A HAS-A B
  31. } ;
  32.  
  33. //... Body ( is an ABC  )
  34. class B
  35. {
  36.     .
  37.     .
  38.     virtual B_f1 ( ) = 0 ;
  39.     virtual B_f2 ( ) ;
  40.     .
  41.     .
  42. } ;
  43.  
  44. Class AÆs purpose is to shield clients from the details of class B and
  45. all it does is merely forward requests to B. Class B is an abstract
  46. base class for class C, D and E. Classes C,D and E implement the
  47. functions B_f1, B_f2 etc.. However, class E has its own member
  48. functions. Hence class E would look something like this - 
  49.  
  50. class E : public B
  51. {
  52.     .
  53.     .
  54.     B_f1 ( )  { } ; 
  55.     B_f2 ( ) { } ;
  56.     E_f1 ( ) { } ;
  57.     .
  58.     .
  59. } ;
  60.  
  61. I would like to invoke E_f1() using the b pointer in class A. Hence
  62. inside A, I would like to do the following - 
  63. class A
  64. {
  65.     .
  66.     .
  67.     A_f3 ( ) { b->E_f1 () ; } ;
  68. } ;
  69.  
  70. This WILL NOT work since we have a B* and E_f1() is not a member
  71. function in class B.
  72. We could force this by using a cast as follows -  
  73. class A
  74. {
  75.     .
  76.     .
  77.     A_f3 ( ) { E* e = (E*)b ; e->E_f1 ( ) ; } ;
  78. } ;
  79. but I have make sure that b points to a valid E object inside A_f3 (). 
  80.  
  81. Is there any elegant way to achieve the above without having to cast or
  82. create a new Handle/Body pair like the following -
  83. class A2 
  84. {
  85.     .
  86.     .
  87.     private:
  88.     E*   e ;
  89. };
  90.  
  91. If anyone has any advice to offer, I would be MOST appreciative.
  92.  
  93. Thanks,
  94. -- TJ 
  95.  
  96. ***********************************************************************
  97. *
  98.  
  99.  
  100.  
  101.  
  102.